package com.example.springfortestspace;

import cn.hutool.core.date.DateUtil;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther：FKM
 * @Date：2024/5/2015:06
 */
public class LogUtil {
   public static void WriteToLogFile(String content){
        try{
            String format = DateUtil.format(new Date(), "HH:mm:ss");
            format += "------当前时间-----";
            String finalFormat = format;
            Thread thread = new Thread(() ->{
                writeContext(finalFormat +content);
            });
            thread.start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    static void writeContext(String content){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = dateFormat.format(new Date());
        String fileName = "/data/soft/jarlog/log_" + currentDate + ".txt";
        try (PrintWriter writer = new PrintWriter(new FileWriter(fileName, true))) {
            writer.println(content);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
